package com.paishen.peiniwan.module.date;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.ui.EaseChatFragment;
import com.paishen.common.dialog.BottomMenuDialog;
import com.paishen.common.dialog.BottomMenuDialog.BottomMenu;
import com.paishen.common.dialog.LoadingDialog;
import com.paishen.common.util.BaseUtil;
import com.paishen.common.util.DateUtil;
import com.paishen.common.util.LogUtil;
import com.paishen.common.widget.TitleLayout;
import com.paishen.peiniwan.ActivityCollector;
import com.paishen.peiniwan.App;
import com.paishen.peiniwan.BaseActivity;
import com.paishen.peiniwan.BusEvent;
import com.paishen.peiniwan.C;
import com.paishen.peiniwan.R;
import com.paishen.peiniwan.database.util.ModelConverter;
import com.paishen.peiniwan.frame.PnwOkCallbackAdapter;
import com.paishen.peiniwan.frame.http.HttpResp;
import com.paishen.peiniwan.frame.http.PnwHttpManager;
import com.paishen.peiniwan.frame.http.entity.Balance;
import com.paishen.peiniwan.frame.http.entity.Book;
import com.paishen.peiniwan.frame.http.entity.DateInfo;
import com.paishen.peiniwan.frame.http.entity.DatePrice;
import com.paishen.peiniwan.frame.http.entity.User;
import com.paishen.peiniwan.frame.http.response.HttpRespData;
import com.paishen.peiniwan.frame.widget.StarView;
import com.paishen.peiniwan.module.message.EaseChatActivity;
import com.paishen.peiniwan.module.profile.ProfileInfoActivity;
import com.squareup.okhttp.Request;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import de.greenrobot.event.EventBus;

public class DateInfoSimpleActivity extends BaseActivity {

    public static final String EXTRA_DATE_INFO = "extra_date_info";
    private Book book;
    private DateInfo dateInfo;
    private User other;
    private String[] date_status;

    private TitleLayout layTitle;
    private View layState, layGift;
    private ImageView imgAvatar, imgGift;
    private TextView txSubject, txNickname, txTime, txLocation, txState, txGift;
    private StarView viewStar;
    private Button btnConfirm, btnRefuse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        dateInfo = (DateInfo) getIntent().getSerializableExtra(EXTRA_DATE_INFO);
        if (dateInfo == null || dateInfo.book == null || (book = dateInfo.book.get(0)) == null) {
            BaseUtil.showToast(R.string.error_data);
            finish();
            return;
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date_info_simple);

        other = book.user;
        date_status = getResources().getStringArray(R.array.date_status);

        layTitle = (TitleLayout) findViewById(R.id.lay_title);
        txState = (TextView) findViewById(R.id.tx_state);
        layState = findViewById(R.id.lay_state);
        imgAvatar = (ImageView) findViewById(R.id.img_avatar);
        txSubject = (TextView) findViewById(R.id.tx_subject);
        txNickname = (TextView) findViewById(R.id.tx_nickname);
        txTime = (TextView) findViewById(R.id.tx_time);
        txLocation = (TextView) findViewById(R.id.tx_location);
        viewStar = (StarView) findViewById(R.id.view_star);
        btnConfirm = (Button) findViewById(R.id.btn_up);
        btnRefuse = (Button) findViewById(R.id.btn_refuse);
        layGift = findViewById(R.id.lay_gift);
        txGift = (TextView) findViewById(R.id.tx_gift);
        imgGift = (ImageView) findViewById(R.id.img_gift);

        findViewById(R.id.lay_other_info).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), ProfileInfoActivity.class);
                intent.putExtra(ProfileInfoActivity.EXTRA_SHOWN_USER_ID, dateInfo.user.id);
                v.getContext().startActivity(intent);
            }
        });

        View.OnClickListener l = new View.OnClickListener() {
            private long timestamp;

            @Override
            public void onClick(View v) {
                final long curr = System.currentTimeMillis();
                if (curr - timestamp < 800) return;
                timestamp = curr;
                switch (v.getId()) {
                    case R.id.btn_up:
                        requestOrderCancelOrder(true);
                        break;
                    case R.id.btn_refuse:
                        requestOrderCancelOrder(false);
                        break;
                }
            }
        };

        btnConfirm.setOnClickListener(l);
        btnRefuse.setOnClickListener(l);

        Glide.with(this).load(dateInfo.user.head_img_url).bitmapTransform(App.i().getCropCircleTransformation())
                .into(imgAvatar);
        viewStar.setLightStarCount(dateInfo.user.good_rate);
        txNickname.setText(dateInfo.user.nickname);

        Glide.with(this).load(dateInfo.gift.image).bitmapTransform(App.i().getCropCircleTransformation())
                .into(imgGift);
        txGift.setText(getString(R.string.date_pay_value_2, String.valueOf((int)dateInfo.gift.price)));

        String date_from = DateUtil.convert(dateInfo.date_from, DateUtil.yyyy_MM_dd_HH_mm_ss, DateUtil.yyyy_MM_dd_HH_mm);
        txTime.setText(date_from);
        txLocation.setText(dateInfo.address);
        if (dateInfo.subject_type != null && dateInfo.subject_type.length > 0) {
            txSubject.setText(dateInfo.subject_type[0]);
        }

        layState.setVisibility(View.VISIBLE);
        txState.setText(date_status[C.date.WAIT_RESPONSE_1]);
        txState.setTextColor(0xFFFC4E52);
        layState.setBackgroundResource(R.mipmap.date_state_4);

        layTitle.pushContent(makeBackView(), getString(R.string.date_info), null, false);

        boolean isSelfPay = dateInfo.user.id == App.i().getUser().getId();
        findViewById(R.id.lay_up).setVisibility(isSelfPay ? View.GONE : View.VISIBLE);
    }


    private void requestOrderCancelOrder(final boolean accept) {
        final LoadingDialog dlgLoading = new LoadingDialog.Builder(this).create();
        dlgLoading.show();

        PnwHttpManager.Order.modifySingle(TAG, dateInfo.id, accept ? 1: 0 , new PnwOkCallbackAdapter<HttpResp>() {
            @Override
            public void onResponse(HttpResp response, boolean fresh) {
                super.onResponse(response, fresh);
                dlgLoading.dismiss();
                if (response.errcode == C.http.RESULT_SUCCESS) {
                    BaseUtil.showToast(accept ? R.string.date_future_come_true : R.string.date_future_lost);
                    finish();
                } else if (fresh && response.errcode != C.http.RESULT_LOGIN_EXPIRE &&
                        response.errcode != C.http.RESULT_LOGIN_OFFLINE) {
                    BaseUtil.showToast(response.errmsg);
                }
            }

            @Override
            public void onFailure(Request request, Exception e) {
                super.onFailure(request, e);
                dlgLoading.dismiss();
            }
        });
    }

}
